package com.example.myapplication;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class MapTask implements Runnable {
    Handler handler;

    public void setHandler(Handler handler) {
        this.handler = handler;
    }

    @Override
    public void run() {
        //获得网络数据
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        List<HashMap<String, String>> retList = new ArrayList<HashMap<String, String>>();
        try {
            Document doc = Jsoup.connect("https://www.usd-cny.com/bankofchina.htm").get();
            String title = doc.title();
            Elements trs = doc.getElementsByTag("tr");
            for (Element tr : trs) {
                Elements tds = tr.getElementsByTag("td");
                if (tds.size() > 0) {
                    String td1 = tds.get(0).text();
                    String td2 = tds.get(5).text();
                    HashMap<String, String> map = new HashMap<String, String>();
                    map.put("cname", td1);
                    map.put("cval", td2);
                    retList.add(map);
                }
            }
            Message msg = handler.obtainMessage(5, retList);
            handler.sendMessage(msg);//handler 可以理解为快递系统
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

